<?php

namespace backend\utils;

use common\models\User;
use Yii;

/**
 * Description of BeginRequest
 *
 * @author zhuxiaohui
 */
class Request
{
    public static function requireLogin()
    {
        if (!stristr(Yii::$app->request->pathInfo, 'site/login')
            && !stristr(Yii::$app->request->pathInfo, "site/captcha")
            && !stristr(Yii::$app->request->pathInfo, "api")
            && !stristr(Yii::$app->request->pathInfo, "site/oauth-login")
            && !stristr(Yii::$app->request->pathInfo, "site/bound")
            && !stristr(Yii::$app->request->pathInfo, "site/ajax-bound")
        ) {
            if (Yii::$app->user->isGuest) {
                Yii::$app->user->loginRequired();
                Yii::$app->end();
            } else {
                if (!stristr(Yii::$app->request->pathInfo, "site/update-password")
                    && (Yii::$app->user->identity->password == md5('123456')
                        || Yii::$app->user->identity->password == md5('tg50!%#'))) {
                    return Yii::$app->response->redirect(['/site/update-password']);
                }
            }
        }
    }

    public static function handleAction()
    {
        if (!Yii::$app->user->isGuest) {
            $model = User::findIdentity(Yii::$app->user->id);
            if (intval($model->user_type) === 3) {
                $expire_time = Yii::$app->session[Yii::$app->user->authTimeoutParam] - Yii::$app->user->authTimeout + 300;
            } else {
                $expire_time = Yii::$app->session[Yii::$app->user->authTimeoutParam];
            }
            $model->expire_time = $expire_time;
            $model->save();
        }
    }


}

